package com.cptravel.activity.order;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.core.api.ApiListenrDefaultImpl;
import com.core.api.B5MBaseResponse;
import com.core.controller.B5MBaseActivity;
import com.core.util.B5MActivityHelper;
import com.cptravel.R;
import com.cptravel.api.CancelorderRequest;
import com.cptravel.api.GetorderRequest;
import com.cptravel.api.GetorderResponse;
import com.cptravel.api.XiangxiRequest;
import com.cptravel.bean.Order;
import com.cptravel.tool.OrderStatus;

public class OrderDetailActivity extends B5MBaseActivity
{
    private TextView tv_no, tv_product, tv_user, tv_money, tv_datetime, tv_status;
    private Button bt_modify, bt_cancel;
    private Order mOrder;
    private CancelorderRequest mCancelorderRequest;
    private XiangxiRequest xiangxiRequest;
    private GetorderRequest getorderRequest;

    @Override
    protected String getHeadTitle()
    {
        // TODO Auto-generated method stub
        return "订单详情";
    }

    @Override
    public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.order_detail;
    }

    @Override
    public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
        tv_no = (TextView) findViewById(R.id.tv_no);
        tv_product = (TextView) findViewById(R.id.tv_product);
        tv_user = (TextView) findViewById(R.id.tv_user);
        tv_money = (TextView) findViewById(R.id.tv_money);
        tv_datetime = (TextView) findViewById(R.id.tv_datetime);
        tv_status = (TextView) findViewById(R.id.tv_status);
        bt_modify = (Button) findViewById(R.id.bt_modify);
        bt_cancel = (Button) findViewById(R.id.bt_cancel);
    }

    @Override
    public void doInitDataes()
    {
        // TODO Auto-generated method stub
        mOrder = (Order) getIntent().getSerializableExtra("order");
        initData();
        mCancelorderRequest = new CancelorderRequest(new ApiListenrDefaultImpl()
        {
            @Override
            public void onSuccess(B5MBaseResponse b5mBaseResponse)
            {
                // TODO Auto-generated method stub
                super.onSuccess(b5mBaseResponse);
                bt_cancel.setEnabled(true);
                finish();
            }
        }, mProgressBar);
        getorderRequest = new GetorderRequest(new ApiListenrDefaultImpl()
        {
            @Override public void onSuccess(B5MBaseResponse b5mBaseResponse)
            {
                super.onSuccess(b5mBaseResponse);
                mOrder = ((GetorderResponse) b5mBaseResponse).order;
            }
        }, mProgressBar);
    }

    private void initData()
    {
        tv_no.setText(String.format("订单编号:%s", mOrder.orderid));
        tv_product.setText(String.format("订单商品:%s", mOrder.pname));
        tv_user.setText(String.format("收货人:%s", mOrder.contact));
        tv_money.setText(String.format("订单金额:%s", Double.valueOf(mOrder.pprice)*Double.valueOf(mOrder.number)));
        tv_datetime.setText(String.format("下单时间:%s", mOrder.createtime));
        tv_status.setText(String.format("订单状态:%s", OrderStatus.getOrderStatusDesc(Integer.valueOf(mOrder.state))));
    }

    @Override
    public void doAfter()
    {
        // TODO Auto-generated method stub
        getorderRequest.orderid = mOrder.orderid;
        getorderRequest.start();
    }

    public void updateOrder(View view)
    {
        if (mOrder == null)
            return;
        Bundle bundle = new Bundle();
        bundle.putSerializable("order", mOrder);
        if ("2".equals(mOrder.classid))//jingqu
        {
            B5MActivityHelper.startActivity(mActivity, BookJingQuActivity.class, bundle);
        }
        else
        {
            B5MActivityHelper.startActivity(mActivity, BookJiuDianActivity.class, bundle);
        }
    }

    public void cancelOrder(View view)
    {
        view.setEnabled(false);
        mCancelorderRequest.orderid = mOrder.orderid;
        mCancelorderRequest.start();
    }
}
